import Mock from "mockjs";

Mock.mock("/api/dict/tree", "post", (name, filter) => {
  return Mock.mock({
    success: true,
    msg: "操作成功！",
    code: 0,
    result: {
      tree:[{
        value: "15",
        label: "内蒙古",
        children: [
          {
            value: "03",
            label: "乌海市",
            children: [
              { value: "02", label: "海勃湾区" },
              { value: "03", label: "乌达区" },
              { value: "04", label: "海南区" },
            ],
          },
          {
            value: "01",
            label: "呼和浩特市",
            children: [
              { value: "02", label: "回民区" },
              { value: "03", label: "赛罕区" },
              { value: "04", label: "新城区" },
            ],
          },
        ],
      }],
      role:"6:2|2|2"
    },
    timestamp: "@now",
  });
});

Mock.mock("/api/dict/list", "post", (option) => {
  const bodyStr = option.body
  let name = ""
  if (isJSON(bodyStr)){
    name = JSON.parse(bodyStr).name
  }else{
    name = bodyStr
  }
  if (name == "性别") {
    return {
      success: true,
      msg: "操作成功！",
      code: 0,
      timestamp: "@now",
      result: {
        rows: [
          { code: "1", title: "男" },
          { code: "2", title: "女" },
        ],
        total: 2,
      },
    };
  } else if (name == "爱好") {
    return {
      success: true,
      msg: "操作成功！",
      code: 0,
      timestamp: "@now",
      result: {
        rows: [
          { code: "01", title: "游戏" },
          { code: "02", title: "读书" },
          { code: "03", title: "旅游" },
          { code: "04", title: "运动" },
        ],
        total: 4,
      },
    };
  } else if (name == "行政区划") {
    return {
      success: true,
      msg: "操作成功！",
      code: 0,
      timestamp: "@now",
      result: {
        rows: [
          { code: "150302", title: "海勃湾" },
          { code: "150303", title: "乌达" },
          { code: "150304", title: "海南" },
          { code: "150102", title: "回民" },
          { code: "150103", title: "赛罕" },
          { code: "150104", title: "新城" },
        ],
        total: 6,
      },
    };
  } else {
    return Mock.mock({
      success: true,
      msg: "操作成功！",
      code: 0,
      timestamp: "@now",
      result: {
        "rows|10-20": [{ "code|+1": 1, title: "@cword(3, 5)" }],
        total: 10,
      },
    });
  }
});

Mock.mock('/api/meta/dict', 'post', option=>{
  const body = JSON.parse(option.body)
  if (body.name=='user'){
    return Mock.mock({
      "success": true,
      "msg": "操作成功！",
      "code": 0,
      "result": {
        "rows|100":[{ 
          "id": "@id", 
          name:'@cname',
          'sex|1':['1','2'],
          birthday:'@date',
          roleName:Mock.Random.pick(["1", "2", "3", "4", "5"],1,5),  // 数组内随机一个值
          'position|1':['1','2','3','4','5'],
          number:'@zip',
          mobile:'@zip',
          'addre':{'code|1':['150302','150303','150304','150102','150103','150104'],addre:'@county'},
          'department|1':['150302','150303','150304','150102','150103','150104'],
          "createBy": "@id",
          "createTime": '@datetime',
          "updateBy|!":['','@id'] ,
          "updateTime": '@datetime',
        }],
        "total":100
      },
      "timestamp": '@now',
    });
  }
})


function isJSON(str) {
  if (typeof str == "string") {
    try {
      var obj = JSON.parse(str);
      if (typeof obj == "object" && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      return false;
    }
  }
}